export default function fontmin(selectDom, fontDom) {  
  const font = fontDom
  
  let getText = (n) => {
    function getStrings(n, strings){
      if(n.nodeType == 3) strings.push(n.data);
      else if(n.nodeType == 1){
        for(var m = n.firstChild; m != null; m = m.nextSibling){
          getStrings(m, strings);
        }
      }
    }
    var strings = [];
    getStrings(n, strings);
    return strings.join(" ");
  }
  
  // 取出dom节点中所有的文字
  let text = getText(document.querySelector(`${selectDom}`))
  // 除掉空白字符
  text = text.replace(/\s*/g,"")
  // 个别文字抓不到，手动添加进来
  text = text.concat('耕心无尽無盡首页慧海拾珠流年光影修改信息退出登录頁錄友链关于關於鏈日常技術术後台管理后没有啦暂留言加载更多暫載反馈法违规善灌水广告饋違規廣')
  // 去重文字
  text = [].filter.call(text,(s,i,o)=>o.indexOf(s)==i).join('')  
  // console.log(text)

  let reqFonter = async () => {
    let {data} = await this.$axios({
      method: 'post',
      url: '/fontmaker/fontmin',
      data: {
        text,
        font
      }
    })
  
    if(data.code){
      return this.$message.error('设置失败')
    }
    
    let ifDEV = process.env.NODE_ENV === "development"
    const baseURL = ifDEV ? 'http://localhost:3000' : window.document.location.origin
    
    let styleDom = document.createElement('style')
    styleDom.type = 'text/css'

    let ran = Date.now().toString(32)+Math.floor(Math.random(10000)*10000)
    let fontUrl = `${baseURL}/files/font/${data.font}.ttf?${ran}`
    let cssText = `
      @font-face {
        font-family: "${data.font}";
        src: url("${fontUrl}") format("truetype");
      }
      p, h1, h2, h3, h4, h5, li, div, span {
        font-family: "${data.font}";
      }
      `
    styleDom.appendChild(document.createTextNode(cssText));
    document.getElementsByTagName('head')[0].appendChild(styleDom)
  }

  reqFonter()
}